package com.start.dataSource.mappers;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.dataSource.entity.StartGenTableColumn;
import com.start.dataSource.entity.StartSourceDataTable;
import com.start.dataSource.entity.StartSourceGenTable;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;


@Mapper
public interface StartDataTableMapper extends BaseMapper<StartSourceDataTable> {
    String SELECT_SYSTEM_TABLE = " SELECT  " +
            "   null id,  " +
            "  `tables`.`TABLE_NAME` AS `table_name`,  " +
            "   null table_prefix,  " +
            "   null package_name,  " +
            "   null module_name,  " +
            "   null menu_parent_id,  " +
            "   null author,  " +
            "   1 is_create,  " +
            "  null crud_option,  " +
            "   `tables`.`TABLE_COMMENT` `table_comment`,  " +
            "    if(IFNULL(`tables`.`ENGINE`,'view')!='view','table','view') table_type,  " +
            "   `tables`.`ENGINE` AS `engine_name`,  " +
            "  `tables`.`CREATE_TIME` AS `create_time`,  " +
            "  null create_by,  " +
            "  null update_by,  " +
            "  null update_time,  " +
            "  `tables`.`TABLE_SCHEMA` AS `data_source_key_flag`  " +
            "FROM  " +
            "  `information_schema`.`TABLES`  " +
            "WHERE  " +
            "  tables.`TABLE_SCHEMA` =(select DATABASE())";

    @DS("#dataSourcesKey")
    @Select(SELECT_SYSTEM_TABLE + " ${ew.customSqlSegment}")
    Page<StartSourceDataTable> pageSystemTable(@Param("page") Page page, @Param(Constants.WRAPPER) Wrapper<StartSourceDataTable> wrapper);


    @DS("#dataSourcesKey")
    @Delete("DROP TABLE ${tableName} ;")
    void deleteTable(@Param("tableName") String tableName);

    @DS("#dataSourcesKey")
    @Select("    select column_name    columnName,  " +
            "               data_type      dataType,  " +
            "               column_comment columnComment,  " +
            "               column_key     columnKey,  " +
            "               extra,  " +
            "               is_nullable as isNullable,  " +
            "               column_type as columnType  " +
            "        from information_schema.columns  " +
            "        where table_name = #{tableName}  " +
            "          and table_schema = (select database())  " +
            "        order by ordinal_position")
    List<StartGenTableColumn> queryByTableColumn(String dataSourcesKey, @Param("tableName") String tableName);

    @DS("#dataSourcesKey")
    @Select("select count(1)  num from `information_schema`.`TABLES` ist where ist.`TABLE_SCHEMA` =(select DATABASE()) and ist.TABLE_NAME=#{tableName}")
    Integer getTableCount(String dataSourcesKey, @Param("tableName") String tableName);

    @DS("#dataSourcesKey")
    @Select("create table (${columnSql})")
    void createTable(String dataSourcesKey, @Param("columnSql") String columnSql);

    @DS("#dataSourcesKey")
    @Select("ALTER TABLE #{tableName}  ${columnSql}")
    void addTableColumn(String dataSourcesKey, @Param("tableName") String tableName, @Param("columnSql") String columnSql);

    @DS("#dataSourcesKey")
    @Select("ALTER TABLE #{tableName} DROP COLUMN ${columnName}")
    void deleteTableColumn(String dataSourcesKey, @Param("tableName") String tableName, @Param("columnName") String columnName);

    @Select("  SELECT `tables`.`TABLE_NAME`      AS `table_name`,  " +
            "               `tables`.`ENGINE`          AS `engine_name`,  " +
            "               `tables`.`TABLE_COMMENT`   AS `table_comment`,  " +
            "               `tables`.`TABLE_COLLATION` AS `table_collation`,  " +
            "               `tables`.`CREATE_TIME`     AS `create_time`,  " +
            "               `tables`.`TABLE_SCHEMA`    AS `table_schema`  " +
            "        FROM `information_schema`.`TABLES`  " +
            "        where table_schema = (select dataBase())  " +
            "          and `tables`.`TABLE_NAME` = #{tableName}  " +
            "        order by `tables`.`CREATE_TIME` desc LIMIT 1")
    StartSourceGenTable getDbTable(String dataSourcesKey,@Param("tableName" ) String tableName);
}
